<?php

namespace App\Admin\Controllers\Vote\Season;

use App\Admin\Actions\Batches\BatchDisable;
use App\Admin\Actions\Batches\BatchEnable;
use App\Admin\Actions\GoBackButton;
use App\Admin\Actions\Season\PosterPreview;
use App\Admin\Traits\WithUploads;
use App\Enums\FontFamily;
use App\Enums\LayerPosition;
use App\Enums\LayerType;
use App\Models\Design;
use App\Models\Gift;
use App\Models\Season;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;

class DesignController extends Controller
{
    use WithUploads;

    public function index(Content $content, Season $season): Content
    {
        return $content->header('海报设计')
            ->description($season->title)
            ->body($this->grid($season));
    }

    public function grid(Season $season): Grid
    {
        $model = Design::ofAdmin()
            ->where('season_id', $season->getKey())
            ->ordered();

        return Grid::make($model, function (Grid $grid) use ($season) {
            if (Admin::user()->isAdministrator()) {
                $grid->column('application.name', '所属应用');
                $grid->disableCreateButton();
            }

            $grid->tools([new GoBackButton, new PosterPreview($season)]);

            $grid->batchActions([
                new BatchEnable(Gift::class),
                new BatchDisable(Gift::class),
            ]);
            $grid->column('type', '图层类型')
                ->using(LayerType::TYPE_MAP);
            $grid->column('order')
                ->orderable();
            $grid->column('status')
                ->bool();
            $grid->column('created_at');
        });
    }

    public function create(Content $content, Season $season): Content
    {
        return $content->header('新增图层')
            ->description($season->title)
            ->body($this->form($season));
    }

    public function form(Season $season): Form
    {
        return Form::make(Design::ofAdmin(), function (Form $form) use ($season) {
            $form->hidden('season_id')
                ->value($season->getKey());

            $form->radio('type', '图层类型')
                ->options(LayerType::TYPE_MAP)
                ->required()
                ->default(LayerType::BACKGROUND->value)
                ->help('背景层、遮罩层和文字层完全自定义。姓名、头像和二维码由系统调取')
                ->when(LayerType::BACKGROUND->value, function (Form $form) {
                    $this->cover($form, 'config.background.cover', '背景图')
                        ->width(4)
                        ->dimensions(['width' => 750, 'min_height' => 750])
                        ->help('图片尺寸请使用<span style="color:red">750px</span>宽度图片，高度最低750px')
                        ->rules('required_if:type,'.LayerType::BACKGROUND->value, [
                            'required_if' => '背景图片必须上传',
                        ]);
                })
                ->when(LayerType::AVATAR->value, function (Form $form) {
                    $form->number('config.avatar.size', '头像尺寸')
                        ->help('图片宽度，单位为像素，请按照750px画布调整')
                        ->rules('required_if:type,avatar', [
                            'required_if' => '图片宽度必须',
                        ]);
                })
                ->when(LayerType::IMAGE->value, function (Form $form) {
                    $this->cover($form, 'config.image.cover', '遮罩图层')
                        ->width(4)
                        ->accept('png')
                        ->dimensions(['width' => 750, 'min_height' => 750])
                        ->help('遮罩层请使用<span style="color:red">PNG</span>图片，把选手照片进行修正的部分，请使用透明层，图片尺寸请使用<span style="color:red">750px</span>宽度，高度最低750px')
                        ->rules('required_if:type,'.LayerType::IMAGE->value, [
                            'required_if' => '遮罩图片必须上传',
                        ]);
                    $form->number('config.image.size', '遮罩图层尺寸')
                        ->default(750)
                        ->help('图片宽度，单位为像素，请按照750px画布调整')
                        ->rules('required_if:type,'.LayerType::IMAGE->value, [
                            'required_if' => '图片宽度必须',
                        ]);
                })
                ->when(LayerType::NAME->value, function (Form $form) {
                    $form->radio('config.name.font_family', '文字字体')
                        ->options(FontFamily::FAMILY_MAP)
                        ->default(FontFamily::NORMAL->value);
                    $form->number('config.name.font_size', '文字大小')
                        ->default(40);
                    $form->color('config.name.font_color', '文字颜色')
                        ->default('#000000');
                    $form->color('config.name.stroke_color', '描边颜色')
                        ->default('#FFFFFF');
                    $form->number('config.name.stroke_width', '描边宽度')
                        ->default(0);
                    $form->radio('config.name.text_align', '水平对齐')
                        ->options([
                            'center' => '居中对齐',
                            'left' => '左对齐',
                            'right' => '右对齐',
                        ])
                        ->default('center');
                    $form->radio('config.name.text_valign', '垂直对齐')
                        ->options([
                            'top' => '顶部对齐',
                            'bottom' => '底部对齐',
                            'middle' => '垂直居中',
                        ])
                        ->default('top');
                    $form->number('config.name.text_angle', '文字旋转角度')
                        ->default(0);
                })
                ->when(LayerType::QRCODE->value, function (Form $form) {
                    $form->number('config.qrcode.size', '二维码尺寸')
                        ->help('图片宽度，单位为像素，请按照750px画布调整');
                    $form->radio('config.qrcode.correction', '容错级别')
                        ->options([
                            'L' => 'L',
                            'M' => 'M',
                            'Q' => 'Q',
                            'H' => 'H',
                        ])
                        ->default('M');
                    $form->number('config.qrcode.margin', '二维码边距')
                        ->default(0);
                    $form->color('config.qrcode.background', '背景颜色')
                        ->default('#ffffff');
                    $form->number('config.qrcode.background_alpha', '背景透明度')
                        ->default(100);
                    $form->color('config.qrcode.color', '二维码颜色')
                        ->default('#000000');
                    $form->number('config.qrcode.color_alpha', '二维码透明度')
                        ->default(100);
                })
                ->when(LayerType::TEXT->value, function (Form $form) {
                    $form->text('config.text.text', '文字内容')
                        ->rules('required_if:type,'.LayerType::TEXT->value, [
                            'required_if' => '文字内容必须填写',
                        ]);
                    $form->radio('config.text.font_family', '文字字体')
                        ->options(FontFamily::FAMILY_MAP)
                        ->default(FontFamily::NORMAL->value);
                    $form->number('config.text.font_size', '文字大小')
                        ->default(40);
                    $form->color('config.text.font_color', '文字颜色')
                        ->default('#000000');
                    $form->color('config.text.stroke_color', '描边颜色')
                        ->default('#FFFFFF');
                    $form->number('config.text.stroke_width', '描边宽度')
                        ->default(0);
                    $form->radio('config.text.text_align', '水平对齐')
                        ->options([
                            'center' => '居中对齐',
                            'left' => '左对齐',
                            'right' => '右对齐',
                        ])
                        ->default('center');
                    $form->radio('config.text.text_valign', '垂直对齐')
                        ->options([
                            'top' => '顶部对齐',
                            'bottom' => '底部对齐',
                            'middle' => '垂直居中',
                        ])
                        ->default('top');
                    $form->number('config.text.text_angle', '文字旋转角度')
                        ->default(0);
                });
            $form->switch('status')
                ->default(true);
            $form->divider('图层位置设置');
            $form->radio('position', '图层位置')
                ->help('* 文字图层不适用于此选项')
                ->options(LayerPosition::POSITION_MAP)
                ->required()
                ->default(LayerPosition::TOP_LEFT->value);
            $form->number('x_axis', 'X 轴坐标')
                ->required()
                ->value(0);
            $form->number('y_axis', 'Y 轴坐标')
                ->required()
                ->value(0);
        });
    }

    public function edit(Content $content, Season $season, int $id): Content
    {
        return $content->header('编辑图层')
            ->description($season->title)
            ->body($this->form($season)->edit($id));
    }

    public function update(Season $season, int|string $id): JsonResponse
    {
        return $this->form($season)->update($id);
    }

    public function store(Season $season): JsonResponse
    {
        return $this->form($season)->store();
    }

    public function destroy(Season $season, int|string $id): JsonResponse
    {
        return $this->form($season)->destroy($id);
    }
}
